using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public enum Character {
    Side,
    Deviler,
    Gallan
}

public class PlayerController : MonoSingleton<PlayerController> {

    public Character type;

    public Transform bornPoint;

    public bool allowSwicthInput;

#if UNITY_EDITOR

    public bool ignoreDead;

#endif

    public List<CharacterData> players { get; private set; }

    public CharacterData current { get; private set; }

    public void SetAllowSwicthInput(bool allowSwicthInput) {
        this.allowSwicthInput = allowSwicthInput;
    }

    bool CanSwitchPlay() {
        return !current.isBodyChange;
    }

    public void SetBornPoint(Transform transform) {
        bornPoint = transform;
    }

    public void SetCameraFollow() {
        var arr = FindObjectsOfType<CinemachineVirtualCamera>();
        foreach (var cam in arr) {
            cam.Follow = current.cameraFollow;
        }
    }

    public void Dead() {

#if UNITY_EDITOR
        if (ignoreDead) {
            return;
        }
#endif

        current.Teleport(bornPoint.position);
    }

    public void SwitchCharacter(int index) {

        Character character = (Character)index;

        type = character;
        for (int i = 0, len = players.Count; i < len; i++) {
            var player = players[i];
            if (i == (int)character) {
                player.gameObject.SetActive(true);
                if(current != null) {
                    player.CopyValuesFrom(current);
                }
                current = player;
            } else {
                player.gameObject.SetActive(false);
            }
        }

        SetCameraFollow();
    }

    public void SwitchNextCharacter(bool next = true) {

        var index = (int)type;
        index += next ? 1 : -1;
        if (index >= players.Count)
            index = 0;
        if (index < 0)
            index = players.Count - 1;
        SwitchCharacter(index);
    }


    CharacterData LoadCharacter(Character character) {
        GameObject prefab = Resources.Load<GameObject>(character.ToString());
        GameObject player = Instantiate(prefab, transform);
        return player.GetComponentInChildren<CharacterData>();
    }

    private void Awake() {
        players = new List<CharacterData>();




        current = LoadCharacter(Character.Side);

        players.Add(current);
        players.Add(LoadCharacter(Character.Deviler));
        players.Add(LoadCharacter(Character.Gallan));
        SwitchCharacter((int)type);
        current.Teleport(bornPoint.position);
    }

    void FixedUpdate() {

        /*
if (Input.GetButton("Jump") && isGround && !limitUp) {
            isInputJump = true;
        }

        // 左右移动
        float hor = Input.GetAxis("Horizontal");
        */

        if(!allowSwicthInput) {
            return;
        }

        if (Input.GetKeyUp(KeyCode.C) && current.isGround && !current.isBodyChange) {
            SwitchNextCharacter();
        }

        if (Input.GetKeyUp(KeyCode.X) && current.isGround && !current.isBodyChange) {
            SwitchNextCharacter(false);
        }

    }

}
